package com.xja.web.domain.service.impl;

import com.xja.web.domain.commons.Constant;
import com.xja.web.domain.mapper.WxUserMapper;
import com.xja.web.domain.mapper.ticketCodeMapper;
import com.xja.web.domain.mapper.wx_Scan_ShareMapper;
import com.xja.web.domain.model.WxUser;
import com.xja.web.domain.model.ticketCode;
import com.xja.web.domain.model.wx_Scan_Share;
import com.xja.web.domain.service.WeChatService;
import org.jdom2.CDATA;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

@Service
public class WeChatServiceImpl  implements WeChatService {
    @Autowired
    private WxUserMapper wx;
    @Autowired
    private ticketCodeMapper ticketCodeMapper;
    @Autowired
    private wx_Scan_ShareMapper wx_Scan_ShareMapper;
    @Override
    public String uodateByUserMsg(Element rootElement) {

        String toUserName = rootElement.getChildText("ToUserName");
        String fromUserName = rootElement.getChildText("FromUserName");
        String createTime = rootElement.getChildText("CreateTime");
        String msgType = rootElement.getChildText("MsgType");
        String content = rootElement.getChildText("content");
        String msg="";
        if (msgType.equals("event")){
            String event = rootElement.getChildText("Event");
            String EventKey = rootElement.getChildText("EventKey");
            WxUser wxUser=null;
            try {
                wxUser = Constant.userInfo(fromUserName);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (event.equals("subscribe")){
                System.out.println("这个是订阅事件");
                Element xml = new Element("xml");
                Element articles = new Element("Articles");
                Element item = new Element("item");
                xml.addContent(new Element("ToUserName").addContent(new CDATA(fromUserName)));
                xml.addContent(new Element("FromUserName").addContent(new CDATA(toUserName)));
                xml.addContent(new Element("CreateTime").addContent(new CDATA(System.currentTimeMillis()+"")));
                xml.addContent(new Element("MsgType").addContent(new CDATA("news")));
                xml.addContent(new Element("ArticleCount").addContent("2"));
                item.addContent((new Element("Title").addContent(new CDATA("死了都要爱"))));
                item.addContent((new Element("Description").addContent(new CDATA("爱情"))));
                item.addContent((new Element("PicUrl").addContent(new CDATA("http://adu867g.hn3.mofasuidao.cn/app/img/logo.png"))));
                item.addContent((new Element("Url").addContent(new CDATA(Constant.getUrl("http://adu867g.hn3.mofasuidao.cn/app/extension.html")))));
                articles.addContent(item);
                Element item2 = new Element("item");
                item2.addContent((new Element("Title").addContent(new CDATA("爱你妹"))));
                item2.addContent((new Element("Description").addContent(new CDATA("狗屁"))));
                item2.addContent((new Element("PicUrl").addContent(new CDATA("http://adu867g.hn3.mofasuidao.cn/app/img/rmen.png"))));
                item2.addContent((new Element("Url").addContent(new CDATA("www.baidu.com"))));
                articles.addContent(item2);
                xml.addContent(articles);
                Format format = Format.getCompactFormat();
                format.setEncoding("utf-8");
                XMLOutputter xmlOutputter = new XMLOutputter(format);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                try {
                    xmlOutputter.output(xml,byteArrayOutputStream);
                    msg = byteArrayOutputStream.toString("utf-8");
                    System.out.println(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //////////////////////////////////////////////////////////////////////////////////////////////////////////
                WxUser wxUser1 = wx.selectByOpenId(wxUser.getOpenid());
                if (wxUser1!=null){
                    wxUser.setId(wxUser1.getId());
                    wx.updateByPrimaryKeySelective(wxUser);
                }else {
                    System.out.println(EventKey);
                    if (EventKey.length()>8){
                        String s = EventKey.split("_")[1];
                        WxUser wxUserid = wx.selectByPrimaryKey(Integer.valueOf(s));
                        wx_Scan_Share wx_scan_share1 = new wx_Scan_Share();
                        wx_scan_share1.setScanopenid(wxUser.getOpenid());
                        wx_scan_share1.setShareopenid(wxUserid.getOpenid());
                        wx_Scan_ShareMapper.insert(wx_scan_share1);
                    }
                    wx.insertSelective(wxUser);
                }
                /////////////////////////////////////////////////////////////////////////////////////////通过扫码关注的


            }else if(event.equals("unsubscribe")){
                System.out.println("这个是取消订阅事件");
                WxUser wxUser1 = wx.selectByOpenId(wxUser.getOpenid());
                wxUser1.setSubscribe(0);
                wx.updateByPrimaryKeySelective(wxUser1);
                System.out.println(event);
            }
        }else if(msgType.equals("text")){
            String text = rootElement.getChildText("Content");
            System.out.println("这是文本"+text);
        }

        return msg;
    }

    @Override
    public Map<String, Object> upDateCode(Map<String, Object> map,String type) {
        if (type.equals("share")){
            String ticket="";
            System.out.println(map.get("openid").toString());
            ticketCode ticketCode = ticketCodeMapper.selectByPrimaryKey(map.get("openid").toString());
            long nowTime = System.currentTimeMillis()/1000;
            //如果openid不等于空并且二维码没有过期的话 就直接赋值
            if (ticketCode!=null && (nowTime-ticketCode.getCreatecodetime()<ticketCode.getExpireseconds())){
                    ticket=ticketCode.getTicket();
            }else{
                WxUser wxUser = wx.selectByOpenId(map.get("openid").toString());
                JSONObject qrCode = Constant.getQrCode(wxUser.getId()+"");
                if (qrCode!=null&&qrCode.has("ticket")){
                    ticketCodeMapper.deleteByPrimaryKey(map.get("openid").toString());
                    ticket=qrCode.get("ticket").toString();
                    ticketCode ticketCode1 = new ticketCode();
                    ticketCode1.setCreatecodetime(System.currentTimeMillis()/1000);
                    ticketCode1.setOpenid(map.get("openid").toString());
                    ticketCode1.setTicket(ticket);
                    int i = ticketCodeMapper.insert(ticketCode1);
                }
            }
            map.put("ticket",ticket);
        }
        return map;
    }
}
